package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbBrand;
import com.dongdongshop.service.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dongdongshop.data.Result;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference
    private BrandService brandService;

    private final Logger logger= LoggerFactory.getLogger(BrandController.class);

    @RequestMapping("toIndex")
    public String index(){
        return "admin/index";
    }

    @RequestMapping("toList")
    public String toList(){
        return "admin/brand";
    }


    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(){
        try {
            List<TbBrand> getInfo=brandService.getInfo();
          //  System.out.println("长度kkkkk"+getInfo.size());
            return Result.response(ResultEnum.OK).setData(getInfo);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand tbBrand){
        try {
            int i=brandService.addBrand(tbBrand);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("getBrandById/{id}")
    @ResponseBody
    public Result getBrandById(@PathVariable("id")Long id){
        try {
            TbBrand tbBrand=brandService.getBrandById(id);
            return Result.response(ResultEnum.OK).setData(tbBrand);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateBrandById")
    @ResponseBody
    public Result updateBrandById(TbBrand tbBrand){
        try{
            int i=brandService.updateBrandById(tbBrand);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("deleteBrand")
    @ResponseBody
    public Result deleteBrand(@RequestParam("ids[]")Integer[] ids){
        try {
            int i=brandService.deleteBrand(ids);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
